package com.yonyou.pmclouds.altercategory.process;

import com.yonyou.pmclouds.altercategory.entity.AlterCategoryVO;
import com.yonyou.pmclouds.altercategory.mapper.AlterCategoryMapper;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.ref.itf.RefCodeConst;
import com.yonyou.pmclouds.basecom.ref.itf.ReferenceChecker;

public class AlterCategoryDeleteCheckProcessor implements SingleProcessor<AlterCategoryVO> {

    private ReferenceChecker referenceChecker;
    private AlterCategoryMapper mapper;

    public AlterCategoryDeleteCheckProcessor(ReferenceChecker referenceChecker, AlterCategoryMapper mapper) {
        this.referenceChecker = referenceChecker;
        this.mapper = mapper;
    }

    @Override
    public void process(AlterCategoryVO vo, AlterCategoryVO orig) throws BusinessException {
        if(vo == null) return;
        int childCount = mapper.countChild(vo.getPkAltercategory());
        if(childCount > 0) {
            throw new BusinessException("档案已经存在下级类别，不可删除！");
        }
        if(referenceChecker.checkReferenced(RefCodeConst.ALTERCATEGORY, new String[]{vo.getPkAltercategory()})){
            throw new BusinessException("该档案已被引用，无法删除！");
        }
    }
}
